#include "user.h"
#define LED_P_PORT GPIOB
#define LED_P_PIN LL_GPIO_PIN_0
#define LED_N_PORT GPIOB
#define LED_N_PIN LL_GPIO_PIN_1
#define LED_INT	EXTI_LINE1
#define TEST_LED_PORT GPIOC
#define TEST_LED_PIN LL_GPIO_PIN_9
typedef struct
{
	uint64_t StartTick;
	Timer_t Timer;
	uint32_t Passms;
	u8 State;
}LEDSensor_CtrlStruct;

static LEDSensor_CtrlStruct gLEDSensor;

void LEDSensor_Int(PV_Union pData)
{
	uint8_t IsError;
	uint64_t EndTick;
	if (IORead(LED_P_PORT, LED_P_PIN, &IsError))
	{
		return;
	}
//	if (IORead(LED_N_PORT, LED_N_PIN, &IsError))
//	{
//		return;
//	}
	EXTI_OnOff(LED_INT, 0);
	EndTick = Timer_GetUsecFromPoweron();
	gLEDSensor.Passms = (EndTick - gLEDSensor.StartTick)/1000;
//	if (gLEDSensor.Passms < 500)
//	{
//		Timer_Start(&gLEDSensor.Timer, 0, (500 - gLEDSensor.Passms) * 1000, 0);
//	}
//	else
//	{
//		Timer_Start(&gLEDSensor.Timer, 0, 100, 0);
//	}
	Timer_Start(&gLEDSensor.Timer, 0, 100, 0);
}

void LEDSensor_Run(PV_Union pData)
{
	uint8_t IsError;
	switch(gLEDSensor.State)
	{
	case 0:
		IOWrite(LED_P_PORT, LED_P_PIN, 0);
		IOWrite(LED_N_PORT, LED_N_PIN, 1);
//		LL_GPIO_SetPinOutputType(LED_N_PORT, LED_N_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
		gLEDSensor.State++;
		Timer_Start(&gLEDSensor.Timer, 0, 1, 0);
		break;
	case 1:
		EXTI_OnOff(LED_INT, 1);
		gLEDSensor.StartTick = Timer_GetUsecFromPoweron();
		gLEDSensor.State++;
		gLEDSensor.Passms = 500;
		Timer_Start(&gLEDSensor.Timer, 0,500000, 0);
//		LL_GPIO_SetPinOutputType(LED_N_PORT, LED_N_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
		LL_GPIO_SetPinMode(LED_N_PORT, LED_N_PIN, LL_GPIO_MODE_INPUT);
		DBG_INFO("%d", IORead(LED_N_PORT, LED_N_PIN, &IsError));
//		LL_GPIO_SetPinOutputType(LED_P_PORT, LED_P_PIN, LL_GPIO_OUTPUT_OPENDRAIN);
//		LL_GPIO_SetPinMode(LED_P_PORT, LED_P_PIN, LL_GPIO_MODE_INPUT);
		break;
	case 2:
		EXTI_OnOff(LED_INT, 0);
		Timer_Start(&gLEDSensor.Timer, 0, 1000, 0);
		gLEDSensor.State++;
		DBG_INFO("%d %dms", IORead(LED_N_PORT, LED_N_PIN, &IsError), gLEDSensor.Passms);
		break;
	default:
//		LL_GPIO_SetPinOutputType(LED_N_PORT, LED_N_PIN, LL_GPIO_OUTPUT_PUSHPULL);
		LL_GPIO_SetPinMode(LED_N_PORT, LED_N_PIN, LL_GPIO_MODE_OUTPUT);
//		LL_GPIO_SetPinOutputType(LED_P_PORT, LED_P_PIN, LL_GPIO_OUTPUT_PUSHPULL);
//		LL_GPIO_SetPinMode(LED_P_PORT, LED_P_PIN, LL_GPIO_MODE_OUTPUT);
		IOWrite(LED_P_PORT, LED_P_PIN, 1);
		IOWrite(LED_N_PORT, LED_N_PIN, 0);
		Timer_Start(&gLEDSensor.Timer, 0, 100000, 0);
		gLEDSensor.State = 0;
		break;
	}
}

void LEDSensor_Init(void)
{
	LL_GPIO_InitTypeDef GPIO_InitStructure;
	EXTI_Register(LED_N_PORT, LED_INT, LL_GPIO_OUTPUT_PUSHPULL, LL_GPIO_PULL_NO, LEDSensor_Int, NULL);
	GPIO_InitStructure.Pin = LED_P_PIN;
	GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_LOW;
	GPIO_InitStructure.Mode = LL_GPIO_MODE_OUTPUT;
	GPIO_InitStructure.Pull = LL_GPIO_PULL_DOWN;
	GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
	GPIO_InitStructure.Alternate = 0;
	LL_GPIO_Init( LED_P_PORT, &GPIO_InitStructure );
//	GPIO_InitStructure.Pin = TEST_LED_PIN;
//	LL_GPIO_Init( TEST_LED_PORT, &GPIO_InitStructure );
	GPIO_InitStructure.Pin = LED_N_PIN;
	GPIO_InitStructure.Mode = LL_GPIO_MODE_OUTPUT;
	GPIO_InitStructure.Pull = LL_GPIO_PULL_NO;
	LL_GPIO_Init( LED_N_PORT, &GPIO_InitStructure );
	IOWrite(LED_P_PORT, LED_P_PIN, 1);
	IOWrite(LED_N_PORT, LED_N_PIN, 0);
//	IOWrite(TEST_LED_PORT, TEST_LED_PIN, 1);
	Timer_Register(&gLEDSensor.Timer, LEDSensor_Run, NULL);
	gLEDSensor.State = 0;
	Timer_Start(&gLEDSensor.Timer, 0, 1, 0);
	LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
    LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTB, LL_SYSCFG_EXTI_LINE1);
//	ADC_InitAndCal();
}
